import 'package:flutter/material.dart';

class InkTextButton extends StatefulWidget {
  const InkTextButton({
    super.key,
    this.onTap,
    this.title,
    this.controller,
  });

  final void Function()? onTap;

  final String? title;
  final InkTextButtonController? controller;

  @override
  State<InkTextButton> createState() => _InkTextButtonState();
}

class _InkTextButtonState extends State<InkTextButton> {
  late InkTextButtonController controller;
  @override
  void initState() {
    controller = widget.controller ??
        InkTextButtonController(InkTextButtonValue(
          title: widget.title,
        ));
    var disabledTitle = controller.value.disabledTitle ?? '';
    if (disabledTitle.isEmpty) {
      disabledTitle = widget.title ?? "";
    }
    controller.value = controller.value.copyWith(
      title: widget.title,
      disabledTitle: disabledTitle,
    );
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return ValueListenableBuilder<InkTextButtonValue>(
      builder: (context, value, _) {
        var enabled = value.enabled ?? true;
        String text = value.title ?? '';
        String disabledTitle = value.disabledTitle ?? text;
        if (!enabled) {
          text = disabledTitle;
        }
        return Material(
          child: Ink(
            decoration: BoxDecoration(
              color: (value.enabled ?? true)
                  ? const Color(0xFF4D5EFF)
                  : Colors.grey,
              borderRadius: const BorderRadius.all(
                Radius.circular(4),
              ),
            ),
            child: InkWell(
              onTap: enabled ? widget.onTap : null,
              child: Align(
                alignment: Alignment.center,
                child: Container(
                  padding: const EdgeInsets.only(top: 8, bottom: 8),
                  alignment: Alignment.center,
                  width: double.infinity,
                  child: Text(
                    text,
                    style: const TextStyle(
                      color: Colors.white,
                      fontSize: 17,
                    ),
                  ),
                ),
              ),
            ),
          ),
        );
      },
      valueListenable: controller,
    );
  }
}

class InkTextButtonController extends ValueNotifier<InkTextButtonValue> {
  InkTextButtonController(super.value);
  String get title => value.title ?? '';
  String get disabledTitle => value.disabledTitle ?? title;
  bool get enabled => value.enabled ?? true;
}

class InkTextButtonValue {
  final String? title;
  final bool? enabled;
  final String? disabledTitle;
  InkTextButtonValue({
    this.title,
    this.enabled = true,
    this.disabledTitle,
  });

  InkTextButtonValue copyWith({
    String? title,
    String? disabledTitle,
    bool? enabled,
  }) {
    return InkTextButtonValue(
      title: title ?? this.title,
      enabled: enabled ?? this.enabled,
      disabledTitle: disabledTitle ?? this.disabledTitle,
    );
  }

  @override
  int get hashCode => Object.hash(title, disabledTitle, enabled);

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is InkTextButtonValue &&
          runtimeType == other.runtimeType &&
          title == other.title &&
          disabledTitle == other.disabledTitle &&
          enabled == other.enabled;
}
